package app.igroman.gismeteo.app;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GisDB extends SQLiteOpenHelper {
	public static final String DB_PATH = "/data/data/app.igroman.gismeteo/databases/";
	//public static final String DB_PATH = "/assets/";
	public static final String DB_NAME = "country.sqlite";
	private static final int DB_VER = 1;
	
	//table name
	private static final String DB_TABLE_NAME = "DATA";
	
	private static final String dbColumns[] = new String[] { "name", "i", "content" };
	
	private static Context fContext;
	
	public GisDB(Context pContext) {
		super(pContext, DB_NAME, null, DB_VER);
		fContext = pContext;
		try {
			createDB();
		} catch (IOException ioe) {
			Log.e("GisDB", "checkDB: IOException = " + ioe);
		}
		Log.i("dev", "GisDB: ");
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		Log.i("dev", "onCreate: begin");
		String sql = "CREATE TABLE IF NOT EXISTS " + DB_TABLE_NAME + " ( " + 
						"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
						dbColumns[0] + " TEXT," +
						dbColumns[1] + " TEXT," +
						dbColumns[2] + " TEXT" +
						")";
		Log.d("GisDB", "onCreate: " + sql);
		arg0.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion >= newVersion)
		 return;
		String sql = null;
		if (oldVersion == 1)
			sql = "ALTER TABLE " + DB_TABLE_NAME + " ADD NOTE TEXT;";
		if (oldVersion == 2)
			sql = "";
		Log.d("GisDB", "onUpgrade: " + sql);
		if (sql != null)
			db.execSQL(sql);			
	}
	
	public void createDB() throws IOException {
		Log.i("GisDB", "createDB: ");
		boolean dbExist = checkDB();

		if (!dbExist) {
			this.getReadableDatabase();
			try {
				copyDB();
			} catch (IOException e) {
				throw new Error("Error copying database");
			}
		} else {
			Log.i("GisDB", "Exist");
		}
	}
	
	private boolean checkDB() {
		Log.i("GisDB", "checkDB: ");
		SQLiteDatabase checkBase = null;		
		try {
			String path = DB_PATH + DB_NAME;
			checkBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		} catch (SQLiteException e) {
			Log.e("GisDB", "checkDB: Exception = " + e);
		}
		
		if (checkBase != null) {
			checkBase.close();
		}
		
		return checkBase != null ? true : false;
	}
	
	private void copyDB() throws IOException {
		Log.i("GisDB", "copyDB: ");
		InputStream input = fContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream output = new FileOutputStream(outFileName);
		
		byte[] buffer = new byte[1024];
		int length;
		while ((length = input.read(buffer)) > 0) {
			output.write(buffer, 0, length);
		}
		
		output.flush();
		output.close();
		input.close();
	}

}
